//
//  ArticleManager.m
//  iCarStyle
//
//  Created by liunian on 13-9-10.
//  Copyright (c) 2013年 liunian. All rights reserved.
//

#import "ArticleManager.h"
#import "Article.h"
#import "NewsEntity.h"
#import "TFHpple.h"
#import "HtmlHelper.h"
#import "CEntity.h"
#import "HttpRequest.h"
#import "AppDelegate.h"
#import "CoreDataManager.h"

#define kDEFAULTCACHE   @"kDEFAULTCACHE"
#define kLanuch  @"kLanuch"
#define kToday  @"kToday"

@interface ArticleManager (){
    MKEnvObserverCenter    *_obCenter;
}
@end

static ArticleManager * sharedManager;
@implementation ArticleManager

+ (ArticleManager *)sharedManager{
    if (!sharedManager) {
        sharedManager = [[ArticleManager alloc] init];
    }
    return sharedManager;
}
- (id)init
{
    self = [super init];
    if (self) {
        _obCenter = [[MKEnvObserverCenter alloc] init];
    }
    return self;
}
- (void)addObserver:(id<ArticleManagerDelegate>)observer
{
    [_obCenter addEnvObserver:observer];
}

- (void)removeObserver:(id<ArticleManagerDelegate>)observer
{
    [_obCenter removeEnvObserver:observer];
}


- (void)insertArticle:(Article *)article{
    [[CoreDataManager sharedManager] insertArticle:article];
}

- (void)insertPicture:(PicModel *)picture{
    [[CoreDataManager sharedManager] insertPicture:picture];
}

- (void)insertSinglePic:(SinglePic *)singlePic{
    [[CoreDataManager sharedManager] insertSinglePic:singlePic];
}

- (NSArray *)singlePicsForPicture:(PicModel *)picture{
    return [[CoreDataManager sharedManager] singlePicsForPicture:picture];
}
- (void)getArticles{

}
- (BOOL)isFavArticle:(Article *)article{
    return [[CoreDataManager sharedManager] isFavArticle:article];
}
- (void)favArticle:(Article *)article{
    [[CoreDataManager sharedManager] favArticle:article];
}
- (void)cancelFavArticle:(Article *)article{
    [[CoreDataManager sharedManager] cancelFavArticle:article];
}
- (void)reloadNewsWithReqObject:(ReqObject *)object itemsBlock:(ItemsBlock)block{
    dispatch_async(kBgQueue, ^{
        HtmlHelper *helper = [[HtmlHelper alloc] init];
        [helper paserNewsListHtml:[NSURL URLWithString:object.cUrl] itemsBlock:^(NSArray *items) {
            dispatch_async(dispatch_get_main_queue(), ^{
#if NS_BLOCKS_AVAILABLE
                block(items);
#endif
            });
        }];
    });
}

- (void)reloadPicListReqObject:(ReqObject *)req itemsBlock:(ItemsBlock)block{

    dispatch_async(kBgQueue, ^{
        HtmlHelper *helper = [[HtmlHelper alloc] init];
        [helper paserPicListHtml:[NSURL URLWithString:req.cUrl] itemsBlock:^(NSArray *items) {
            dispatch_async(dispatch_get_main_queue(), ^{
#if NS_BLOCKS_AVAILABLE
                block(items);
#endif
            });
        }];

    });
}
- (void)reloadPicListReqObject:(ReqObject *)req itemsBlock:(ItemsBlock)block updateItemBlock:(ItemsBlock)updateBlock{
    dispatch_async(kBgQueue, ^{
        HtmlHelper *helper = [[HtmlHelper alloc] init];

        [helper paserPicListHtml:[NSURL URLWithString:req.cUrl] callbackBlock:^(NSArray *items) {
            dispatch_async(dispatch_get_main_queue(), ^{
#if NS_BLOCKS_AVAILABLE
                BMLog(@"block---1");
                block(items);
#endif
            });
        } updateItemsBlock:^(NSArray *items) {
            dispatch_async(dispatch_get_main_queue(), ^{
#if NS_BLOCKS_AVAILABLE
                BMLog(@"block---2");
                updateBlock(items);
#endif
            });
        }];
    });

}

//TODO:加载前10页数据
void myFinalizerFunction(){
    NSLog(@"xxx");
}

- (void)cacheTenpages{
    BMLog(@"cacheTenpages");
    NSMutableArray *list = [NSMutableArray arrayWithCapacity:90];
    
    for (int j = 1; j <=90; j++) {
        ReqObject *objReq = [[ReqObject alloc] initWithCatalogType:0 pageNum:j];
        [list addObject:objReq];
    }
    NSInteger index = 0;
    dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
    dispatch_set_context(queue, @"xxx");
    dispatch_set_finalizer_f(queue, &myFinalizerFunction);
    for (ReqObject *object in list) {
        if (![CEntity MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(link == %@)",object.cUrl]]) {
            index++;
            BMLog(@"index:%d", index);
            dispatch_async(queue, ^{
                HtmlHelper *helper = [[HtmlHelper alloc] init];
                [helper paserNewsListHtml:[NSURL URLWithString:object.cUrl] itemsBlock:^(NSArray *items) {

                }];
            });
        }
    }
    
    BMLog(@"waiting");
    dispatch_sync(queue, ^{
        BMLog(@"wait done");
    });
}

- (BOOL)isCache{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kDEFAULTCACHE];
}
- (void)setCache:(BOOL)cacheFinish{
    if (cacheFinish) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kDEFAULTCACHE];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDEFAULTCACHE];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isFirstLaunch{
    NSString *lanuch = [[NSUserDefaults standardUserDefaults] stringForKey:kLanuch];
    if (lanuch == nil) {
        return YES;
    }
    return NO;
}
- (void)setFirstLaunch:(BOOL)firstLaunch{
    if (firstLaunch) {
        [[NSUserDefaults standardUserDefaults] setBool:firstLaunch forKey:kLanuch];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kLanuch];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isTodayClick{
    NSString *today = [[NSUserDefaults standardUserDefaults] stringForKey:kToday];
    NSString *todayStr = [Util stringWithDate:[NSDate date]];
    if ([today isEqualToString:todayStr]) {
        return YES;
    }
    return NO;
}
- (void)setTodayClick:(BOOL)todayClick{
    if (todayClick) {
        NSString *today = [Util stringWithDate:[NSDate date]];
        [[NSUserDefaults standardUserDefaults] setObject:today forKey:kToday];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kToday];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
